package com.qd.panda.info;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.dto.DelDTO;
import com.cdqidi.exception.ApiException;
import com.cdqidi.util.page.PageUtil;
import com.cdqidi.valid.group.DefaultGroup;
import com.cdqidi.valid.group.UpdateGroup;
import com.qd.common.panda.constant.PandaConstant;
import com.qd.common.panda.domain.entity.info.ParkInfoDTO;
import com.qd.common.sys.annotation.DataScope;
import com.qd.common.sys.annotation.OperatorInfo;
import com.qd.common.sys.enums.OperatorType;
import com.qd.common.sys.util.SecurityUtils;
import com.qd.panda.service.info.ParkInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 短期租赁/WEB_车位信息
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/sys/parkInfo/")
@Slf4j
public class ParkInfoController {
    private final ParkInfoService parkInfoService;

    /**
     * 审核车位信息
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PreAuthorize("@ps.hasPermission('system:parkInfo:audit')")
    @OperatorInfo(title = "审核", operator = OperatorType.UPDATE)
    @PutMapping(value = "/audit")
    public ParkInfoDTO audit(@Validated @RequestBody ParkInfoDTO dto) {
        if (null == dto.getAid()) {
            throw new ApiException("车位ID不能为空");
        }
        if (null == dto.getApprovalStatus()) {
            throw new ApiException("审核状态不能为空");
        }
        if (!dto.getApprovalStatus().equals(PandaConstant.APPROVAL_STATUS_1) && !dto.getApprovalStatus().equals(PandaConstant.APPROVAL_STATUS_2)) {
            throw new ApiException("审核状态值错误");
        }
        if (dto.getApprovalStatus().equals(PandaConstant.APPROVAL_STATUS_2)) {
            if (!StringUtils.hasLength(dto.getApprovalResult())) {
                throw new ApiException("审核结果不能为空");
            }
        }
        if (dto.getApprovalStatus().equals(PandaConstant.APPROVAL_STATUS_1)) {
            dto.setApprovalResult("审核通过");
            if (!StringUtils.hasLength(dto.getPlanPic())) {
                throw new ApiException("平面图不能为空");
            }
        }
        dto.setApprovalUser(SecurityUtils.getLoginUser().getUserId());
        dto.setApprovalUnit(SecurityUtils.getLoginUser().getCurrentOrgId());
        dto.setApprovalTime(LocalDateTime.now());
        return parkInfoService.audit(dto);
    }

    /**
     * 删除车位信息
     *
     * @param delDTO delDTO中的ids,主键
     * @return 成功 or 失败
     */
    @PreAuthorize("@ps.hasPermission('system:parkInfo:delete')")
    @OperatorInfo(title = "删除", operator = OperatorType.DELETE)
    @DeleteMapping(value = "/delete")
    public ApiResultWrapper<Object> delete(@Validated @RequestBody DelDTO<String> delDTO) {
        List<String> ids = delDTO.getIds();
        if (ids.size() == 1) {
            parkInfoService.removeById(ids.get(0));
        } else {
            parkInfoService.removeBatchIds(ids);
        }
        return ApiResult.success("删除成功");
    }

    /**
     * 获取车位信息
     *
     * @param id 主键
     * @return 记录
     */
    @GetMapping(value = "/getById")
    public ParkInfoDTO getById(@Validated @NotBlank(message = "ID不能为空") String id) {
        return parkInfoService.getByIdDto(id);
    }

    /**
     * 车位信息列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @DataScope(orgIdAlias = "park_id")
    @GetMapping(value = "/page")
    public IPage<ParkInfoDTO> page(ParkInfoDTO dto) {
        return parkInfoService.page(PageUtil.buildPage(dto), dto);
    }

    /**
     * 添加车位
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PostMapping(value = "/save")
    public ParkInfoDTO save(@Validated({DefaultGroup.class}) @RequestBody ParkInfoDTO dto) {
        dto.setAid(IdWorker.getIdStr());
        dto.setAddTime(LocalDateTime.now());
        dto.setApplicant(SecurityUtils.getLoginUser().getUserId());
        dto.setApprovalStatus(PandaConstant.APPROVAL_STATUS_0);
        return parkInfoService.save(dto);
    }

    /**
     * 修改车位
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PutMapping(value = "/update")
    public ParkInfoDTO update(@Validated({UpdateGroup.class, DefaultGroup.class}) @RequestBody ParkInfoDTO dto) {
        dto.setUpdateTime(LocalDateTime.now());
        dto.setApprovalStatus(PandaConstant.APPROVAL_STATUS_0);
        return parkInfoService.update(dto);
    }

    /**
     * 我添加的车位列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/myPage")
    public IPage<ParkInfoDTO> myPage(ParkInfoDTO dto) {
        dto.setApplicant(SecurityUtils.getLoginUser().getUserId());
        return parkInfoService.myPage(PageUtil.buildPage(dto), dto);
    }

    /**
     * 删除车位照片
     *
     * @param delDTO delDTO中的ids,主键
     * @return 成功 or 失败
     */
    @DeleteMapping(value = "/deletePic")
    public ApiResultWrapper<Object> deletePic(@Validated @RequestBody DelDTO<String> delDTO) {
        List<String> ids = delDTO.getIds();
        if (ids.size() == 1) {
            parkInfoService.removePicById(ids.get(0));
        } else {
            parkInfoService.removePicBatchIds(ids);
        }
        return ApiResult.success("删除成功");
    }

    /**
     * 删除车位产权照片
     *
     * @param delDTO delDTO中的ids,主键
     * @return 成功 or 失败
     */
    @DeleteMapping(value = "/deletePropertyRight")
    public ApiResultWrapper<Object> deletePropertyRight(@Validated @RequestBody DelDTO<String> delDTO) {
        List<String> ids = delDTO.getIds();
        if (ids.size() == 1) {
            parkInfoService.removePropertyRightById(ids.get(0));
        } else {
            parkInfoService.removePropertyRightBatchIds(ids);
        }
        return ApiResult.success("删除成功");
    }
}